package com.easonji.admin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.easonji.admin.mapper.PostMapper;
import com.easonji.admin.model.Post;
import com.easonji.admin.service.IPostService;
import com.easonji.common.base.query.BaseQuery;
import com.easonji.common.base.service.AbstractBaseService;
import com.easonji.common.constant.cache.CacheConsts;
import com.easonji.common.constant.cache.CacheKeyConsts;
import com.easonji.common.factory.PageFactory;
import com.easonji.common.page.PageInfo;
import com.easonji.common.util.JsonUtils;
import com.easonji.common.util.StringUtils;
import com.easonji.core.annotion.CacheRemove;
import com.easonji.core.shiro.ShiroUser;
import com.easonji.core.shiro.util.ShiroUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 职务表 服务实现类
 * </p>
 *
 * @author easonji
 * @since 2018-09-30
 */
@Service
public class PostServiceImpl extends AbstractBaseService<Post> implements IPostService {

    @Resource
    private PostMapper postMapper;

    @Override
    protected JSONObject warp2Json(Post post) {
       JSONObject result = JsonUtils.parseObject(post);

       return result;
    }


    @Override
    public Object queryList(BaseQuery baseQuery){

        // 分页信息
        Page<Post> page = new PageFactory<Post>().defaultPage();
        // 查询条件
        Wrapper<Post> wrapper = new EntityWrapper<>();
        String condition = baseQuery.getCondition();
        if (StringUtils.isNotEmpty(condition)) {
            wrapper.like("p_name", condition);
        }
        List<Post> posts = postMapper.selectPage(page, wrapper);

        return new PageInfo(warp(posts), page.getTotal());
    }

    @Override
    @Cacheable(value = CacheConsts.CONSTS, key = "'" + CacheKeyConsts.POST_NAME + "'+#pId")
    public Object queryRowById(Integer pId) {
        Post post = postMapper.selectById(pId);
        return warp(post);
    }

    @Override
    @CacheRemove(value = CacheConsts.CONSTS, key = { CacheKeyConsts.POST_LIST_ALL,CacheKeyConsts.POST_NAME })
    public Integer save(Post post) {

        ShiroUser shiroUser = ShiroUtils.getUser();
        post.setpCreateUser(shiroUser.getUserId());
        post.setpCreateDate(new Date());
        post.setpUpdateUser(shiroUser.getUserId());
        post.setpUpdateDate(new Date());
        return postMapper.insert(post);
    }

    @Override
    @CacheRemove(value = CacheConsts.CONSTS, key = { CacheKeyConsts.POST_LIST_ALL,CacheKeyConsts.POST_NAME })
    public Integer update(Post postForm) {
        ShiroUser shiroUser = ShiroUtils.getUser();
        postForm.setpUpdateUser(shiroUser.getUserId());
        postForm.setpUpdateDate(new Date());
        Post post = new Post();
        BeanUtils.copyProperties(postForm,post);
        return postMapper.updateById(post);
    }

    @Override
    @CacheRemove(value = CacheConsts.CONSTS, key = { CacheKeyConsts.POST_LIST_ALL,CacheKeyConsts.POST_NAME })
    public Integer delete(Integer pId) {
        Wrapper<Post> wrapper = new EntityWrapper<>();
        wrapper.eq("p_id", pId);
        return postMapper.delete(wrapper);
    }

    @Override
    @Cacheable(value = CacheConsts.CONSTS, key = "'" + CacheKeyConsts.POST_LIST_ALL + "'")
    public Object queryComboList() {
        Wrapper<Post> wrapper = new EntityWrapper<>();
        List<Post> posts = postMapper.selectList(wrapper);
        return posts;
    }

}
